package com.tmy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tmy.Dto.Result;
import com.tmy.entity.RoomStatus;
import com.tmy.mapper.RoomStatusMapper;
import com.tmy.service.RoomStatusService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class RoomStatusServiceImpl implements RoomStatusService {

    @Resource
    private RoomStatusMapper roomStatusMapper;


    /**
     * 查询入住房型数据
     */
    @Override
    public Result queryRoomStatus(RoomStatus roomStatus){
        Result result = new Result();
        try {
            LambdaQueryWrapper<RoomStatus> lqw = new LambdaQueryWrapper<>();
            if (!(roomStatus.getId()==null)){
                lqw.eq(RoomStatus::getId,roomStatus.getId());
            }else {
                if (roomStatus.getRoomId()!=null){
                    lqw.eq(RoomStatus::getRoomId,roomStatus.getRoomId());
                }
                if (roomStatus.getStartDate()!=null){
                    lqw.eq(RoomStatus::getStartDate,roomStatus.getStartDate());
                }
                if (roomStatus.getEndDate()!=null){
                    lqw.eq(RoomStatus::getEndDate,roomStatus.getEndDate());
                }
            }
            List<RoomStatus> statusList =roomStatusMapper.selectList(lqw);
            result.setMsg("房型状态查询成功");
            result.setCode(200);
            result.setData(statusList);
        }catch (Exception err){
            result.setCode(200);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 删除入住房型数据（多个定时任务）
     */
    @Override
    @Scheduled(cron = "0 30 23 * * ?")
    public Result deleteRoomStatus(){
        Result result = new Result();
        try {
            Calendar calendar = Calendar.getInstance();
            String dateTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
            // 获取上一天的时间
            calendar.add(Calendar.DATE, -1);
            LambdaQueryWrapper<RoomStatus> lqw = new LambdaQueryWrapper<>();
            lqw.eq(RoomStatus::getEndDate,dateTime);
            int res = roomStatusMapper.delete(lqw);
            result.setMsg("房型状态删除成功");
            result.setCode(200);
            result.setData(res);
        }catch (Exception err){
            result.setCode(200);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     *删除入住房型数据（单个）
     */
    @Override
    public Result deleteRoomStatu(RoomStatus roomStatus){
        Result result = new Result();
        try {
            roomStatusMapper.deleteById(roomStatus);
            result.setMsg("房型状态删除成功");
            result.setCode(200);
        }catch (Exception err){
            result.setCode(200);
            result.setMsg(err.getCause());
        }
        return result;
    }

}
